package com.navi.loaddata.core.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.navi.loaddata.core.gpEntity.MppLayoutPnlD;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @auther Matthew
 * @Date 2018/3/24
 */
@Service
public class CoordTransformServiceImpl implements CoordTransformService {


  @Override
  public Map<String, Double> big2small(String panelId, List<MppLayoutPnlD> layoutPnlDS, Double glassX, Double glassY)
      throws CorrdTransformException {
    Map<String, Double> dgLines = new HashMap<>(16);
    Map<Integer, List<Double>> pointMap = new HashMap<>(16);
    if (!StringUtils.isEmpty(layoutPnlDS) && layoutPnlDS.size() > 0) {
      layoutPnlDS.forEach(layout -> {
        ArrayList<Double> pointList = new ArrayList<>();
        Double x_sta = layout.getxCoord() * 1000;
        Double xLen = layout.getxAxisLength() * 1000;
        Double x_end = x_sta + xLen;
        Double y_sta = layout.getyCoord() * 1000;
        Double yLen = layout.getyAxisLength() * 1000;
        Double y_end = y_sta - yLen;
        Integer panelNo = layout.getPanelNo();
        pointList.add(x_sta);
        pointList.add(x_end);
        pointList.add(y_sta);
        pointList.add(y_end);
        pointMap.put(panelNo, pointList);
      });
      //判断点在哪个NO
      int[] panelArr = new int[1];
      pointMap.forEach((panelNo,pointList) -> {
        Double x_sta = pointList.get(0);
        Double x_end = pointList.get(1);
        Double y_sta = pointList.get(2);
        Double y_end = pointList.get(3);
        if (glassX >= x_sta && glassX <= x_end && glassY <= y_sta && glassY >= y_end) {
          panelArr[0] = panelNo;
        }
      });
      if (panelArr[0] == 0) {
        throw new CorrdTransformException("该点不在Layout上!");
      }
      List<MppLayoutPnlD> panelLayout = layoutPnlDS.stream().filter(layout -> panelArr[0] == layout.getPanelNo()).collect(Collectors.toList());
      if (panelLayout.size() > 0) {
        Integer gateCnt = panelLayout.get(0).getGateCnt();
        Integer dataCnt = panelLayout.get(0).getDataCnt();
        if (dataCnt == null) {
          throw new CorrdTransformException("该片未维护data");
        }
        if (gateCnt == null) {
          throw new CorrdTransformException("该片未维护gata");
        }
       /* dataCnt = dataCnt * 1000;
        gateCnt = gateCnt * 1000;*/
        Double this_xSta;
        Double this_ySta;
        Double this_xLen;
        Double this_yLen;
        try {
          this_xSta = panelLayout.get(0).getxCoord() * 1000;
          this_ySta = panelLayout.get(0).getyCoord() * 1000;
          this_xLen = panelLayout.get(0).getxAxisLength() * 1000;
          this_yLen = panelLayout.get(0).getyAxisLength() * 1000;
        } catch (Exception e) {
          throw new CorrdTransformException("坐标参数不全!");
        }
        double thisX = Math.abs(glassX - this_xSta);
        double thisY = this_yLen - Math.abs(glassY - this_ySta);
        double this_data = dataCnt * (thisX / this_xLen);
        double this_gate = gateCnt * (thisY / this_yLen);
        BigDecimal b = new BigDecimal(this_data);
        BigDecimal b1 = new BigDecimal(this_gate);
        double data = b.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
        double gate = b1.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();

        double glassS=  (ProcessUtil.getStr2Cnt(panelId.substring(panelId.length()-2,panelId.length()))-1)*dataCnt+data;
        double glassG=  (ProcessUtil.getStr2Cnt(panelId.substring(panelId.length()-2,panelId.length()))-1)*gateCnt+gate;
        dgLines.put("data", data);
        dgLines.put("gate", gate);
        //(chipNo-1)*dateCnt+data
        dgLines.put("glassS",glassS);
        dgLines.put("glassG",glassG);

      } else {
        throw new CorrdTransformException("没有此Panel No!");
      }
    } else {
      throw new CorrdTransformException("layoutPnl 为空");
    }
    return dgLines;
  }

  @Override
  public Map<String, Double> small2big(String panelId, Integer panelNo, List<MppLayoutPnlD> layoutPnlDS, Double data,
                                       Double gate) throws CorrdTransformException {
    Map<String, Double> coord = new HashMap<>(16);
    if (!StringUtils.isEmpty(layoutPnlDS) && layoutPnlDS.size() > 0) {
      List<MppLayoutPnlD> panelLayout = layoutPnlDS.stream().filter(layout -> panelNo.equals(layout.getPanelNo())).collect(Collectors.toList());
      if(panelLayout.size()==0){
        throw new CorrdTransformException("panelNo="+panelNo+" 不存在layout!");
      }
      MppLayoutPnlD mppLayoutPnlD = panelLayout.get(0);
      Double this_xSta = null;
      Double this_ySta = null;
      Double this_xLen = null;
      Double this_yLen = null;
      try {
        this_xSta = mppLayoutPnlD.getxCoord();
        this_ySta = mppLayoutPnlD.getyCoord();
        this_xLen = mppLayoutPnlD.getxAxisLength();
        this_yLen = mppLayoutPnlD.getyAxisLength();
      } catch (Exception e) {
        throw new CorrdTransformException("坐标参数不全!");
      }
      Integer dataCnt = mppLayoutPnlD.getDataCnt();
      Integer gateCnt = mppLayoutPnlD.getGateCnt();
      if (dataCnt == null) {
        throw new CorrdTransformException("该片未维护data!");
      }
      if (gateCnt == null) {
        throw new CorrdTransformException("该片未维护gate!");
      }
      /*dataCnt = dataCnt * 1000;
      gateCnt = gateCnt * 1000;*/
      //计算该data gate在该片的x y
      double thisX = mppLayoutPnlD.getxCoord();
      double thisY = mppLayoutPnlD.getyCoord();
      double x2 = this_xSta + this_xLen;
      double y2 = this_ySta - this_yLen;
      double x = (thisX + (x2 - thisX) / dataCnt * data) * 1000;
      double y = (thisY - (thisY - y2) / gateCnt * gate) * 1000;
      BigDecimal b = new BigDecimal(x);
      BigDecimal b1 = new BigDecimal(y);
      double xx = b.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
      double yy = b1.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
      double glassS=  (ProcessUtil.getStr2Cnt(panelId.substring(panelId.length()-2,panelId.length()))-1)*dataCnt+data;
      double glassG=  (ProcessUtil.getStr2Cnt(panelId.substring(panelId.length()-2,panelId.length()))-1)*gateCnt+gate;
      coord.put("glassX", xx);
      coord.put("glassY", yy);
      coord.put("glassS", glassS);
      coord.put("glassG", glassG);
      coord.put("data", data);
      coord.put("gate", gate);
    } else {
      throw new CorrdTransformException("layoutPnl 为空");
    }
    return coord;
  }

  @Override
  public Map<String, Double> smallDg2smallXy(String PanelId, Integer panelNo, List<MppLayoutPnlD> layoutPnlDS,
      Double data, Double gate) throws CorrdTransformException {
    Map<String, Double> coord = new HashMap<>(16);
    List<MppLayoutPnlD> panelLayout = layoutPnlDS.stream().filter(layout -> panelNo.equals(layout.getPanelNo())).collect(Collectors.toList());
    MppLayoutPnlD mppLayoutPnlD = panelLayout.get(0);
    Integer dataCnt = mppLayoutPnlD.getDataCnt();
    Integer gateCnt = mppLayoutPnlD.getGateCnt();
    Double this_xLen = mppLayoutPnlD.getxAxisLength();
    Double this_yLen = mppLayoutPnlD.getyAxisLength();

    //计算该data gate在该片的x y
    double thisX = this_xLen * (data / dataCnt);
    double thisY = this_yLen * (gate / gateCnt);

    double panelX = thisX - this_xLen/2;
    double panelY = this_yLen/2 - thisY;
    coord.put("panelX", panelX);
    coord.put("panelY", panelY);
    return coord;
  }


  public HashMap<String,String> getDefectTypeMap(String panelId, Integer panelNo, List<MppLayoutPnlD> layoutPnlDS, Long s, Long g, String type, String orientation, String sG)throws CorrdTransformException {
    HashMap<String, String> defectTypeMap = new HashMap<>();
    Long s1 = null;
    Long g1 = null;
    Long s2 = null;
    Long g2 = null;
    String defType=null;
    if (!StringUtils.isEmpty(layoutPnlDS) && layoutPnlDS.size() > 0) {
      List<MppLayoutPnlD> panelLayout = layoutPnlDS.stream().filter(layout -> panelNo.equals(layout.getPanelNo())).collect(Collectors.toList());
      if(panelLayout.size()==0){
        throw new CorrdTransformException("panelNo="+panelNo+" 不存在layout!");
      }
      MppLayoutPnlD mppLayoutPnlD = panelLayout.get(0);

      Integer dataCnt = mppLayoutPnlD.getDataCnt();
      Integer gateCnt = mppLayoutPnlD.getGateCnt();
      if (dataCnt == null) {
        throw new CorrdTransformException("该片未维护data!");
      }
      if (gateCnt == null) {
        throw new CorrdTransformException("该片未维护gate!");
      }
      if ("POINT".equals(type)) {
        s1 = s;
        g1 = g;
        defType="POINT";
      } else if ("LINE".equals(type)) {
        switch (orientation) {
          case "ALL":
            if ("S".equals(sG)) {
              s1 = s;
              s2 = s;
              g1 = Long.valueOf(1);
              g2 = Long.valueOf(gateCnt);
              defType="S_FAIL";
            } else if ("G".equals(sG)) {
              s1 = Long.valueOf(1);
              s2 = Long.valueOf(dataCnt);
              g1 = g;
              g2 = g;
              defType="G_FAIL";
            }
            break;
          case "SHORT":
            if ("S".equals(sG)) {
              s1 = s;
              s2 = s;
              g1 = g;
              g2 = Long.valueOf(1);
              defType="S_SHORT_FAIL";
            } else if ("G".equals(sG)) {
              s1 = Long.valueOf(dataCnt);
              s2 = s;
              g1 = g;
              g2 = g;
              defType="G_SHORT_FAIL";
            }
            break;
          case "LONG":
            if ("S".equals(sG)) {
              s1 = s;
              s2 = s;
              g1 = g;
              g2 = Long.valueOf(gateCnt);
              defType="S_LONG_FAIL";
            } else if ("G".equals(sG)) {
              s1 = Long.valueOf(1);
              s2 = s;
              g1 = g;
              g2 = g;
              defType="S_LONG_FAIL";
            }
            break;
        }

      }
      defectTypeMap.put("PanelS1", String.valueOf(s1));
      defectTypeMap.put("PanelG1", String.valueOf(g1));
      defectTypeMap.put("PanelS2", String.valueOf(s2));
      defectTypeMap.put("PanelG2", String.valueOf(g2));
      defectTypeMap.put("TYPE", defType);


    } else {
      throw new CorrdTransformException("layoutPnl 为空");
    }
    return defectTypeMap;
  }

}
